<template>
  <div class="certification">
    <!-- 表单开始 -->
    <el-form size="small" :inline="true" :model="list" class="demo-form-inline">
      <el-form-item>
        <el-button type="danger" @click="batchDelete">批量删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-input clearable v-model="list.keywords" placeholder="请输入关键字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表单结束 -->
    <!-- 表格开始 -->
    <el-table @select="handleSelect" @select-all="handleSelectAll" size="mini" :fit="true" :data="comments" style="width: 100%;font-size: 12px;">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index" align="center"></el-table-column>
      <el-table-column prop="comment" label="评论内容" align="center"></el-table-column>
      <el-table-column prop="userId" label="评论人编号" align="center"></el-table-column>
      <el-table-column prop="articleId" label="资讯编号" align="center"></el-table-column>
      <el-table-column width="130px" prop="commentTime" label="评论时间" align="center">
        <template slot-scope="scope">
          {{scope.row.commentTime | fmtTime}}
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        align="center"
        filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === '通过' ? 'success' : scope.row.status === '不通过' ? 'danger' : 'info'"
            disable-transitions>{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center">
        <template slot-scope="scope">
          <el-button style="color:#ffba00;" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="scope.row.status === '未审核'" @click="checkHandle(scope.row.id,'通过')" type="text" size="small">通过</el-button>
          <el-button v-if="scope.row.status === '未审核'" style="color:red;" @click="checkHandle(scope.row.id,'不通过')" type="text" size="small">不通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 分页开始 -->
    <div style="float:right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15,20]"
        :page-size="this.list.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!-- 分页结束 -->
  </div>
</template>

<script>
import {mapActions, mapState} from 'vuex'
export default {
  data(){
    return {
      ids:[],
      list:{
          page:1,
          pageSize:10,
        },
      currentPage:1
    }
  },
  computed:{
    ...mapState('comment',['comments','total'])
  },
  methods:{
    ...mapActions('comment',['findAllComment','check','delete','deletes']),
    handleSizeChange(val) {
      this.list.pageSize=val;
      this.findAllComment(this.list)
    },
    handleCurrentChange(val) {
      this.list.page=val;
      this.findAllComment(this.list)
    },
    checkHandle(id,status) {
      let params={
        id,
        status
      }
      this.check(params).then(res=>{
        if(res.status==200){
          this.findAllComment(this.list)
          this.$message({
            type: "success",
            message: res.message
          })
        }else{
            this.$message({
            type: "error",
            message: res.message
          })
        }
      })
    },
    deleteHandle(id) {
      let params={
        id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delete(params).then(res=>{
          if(res.status==200){
            this.findAllComment(this.list)
            this.$message({
              type: "success",
              message: res.message
            })
          }else{
              this.$message({
              type: "error",
              message: res.message
            })
          }
        })
      })
    },
    onSubmit(){
      this.findAllComment(this.list)
    },
    batchDelete(){
      let params={
        ids:this.ids.toString()
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletes(params).then(res=>{
          if(res.status==200){
            this.findAllComment(this.list)
            this.$message({
              type: "success",
              message: res.message
            })
          }else{
              this.$message({
              type: "error",
              message: res.message
            })
          }
        })
      })
    },
    handleSelect(selection) {
      this.ids=[];
      selection.forEach((item)=>{
        this.ids.push(item.id);
      })
    },
    handleSelectAll(selection){
      this.ids=[];
      selection.forEach((item)=>{
        this.ids.push(item.id);
      })
    }
  },
  created(){
    this.findAllComment(this.list)
  }
}
</script>
<style>

</style>